<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;



class Index extends Controller
{
    /**
     * 后台登录页面
     * @return \think\response\View
     */
    public function index()
    {
    	
        if(Session::get('admin_name','admin')&&Session::get('admin_pri','admin')){
    		$this->success('免登陆', url('/main'), []);
    	}
       
    	
    	
    	
    	
        return view('index/login');
    }

    /**
     * 后台登录处理
     * @return \think\response\View
     */
    public function logindo()
    {
    	$username = input('post.username');
        $password = input('post.password');

        $pwd = DB::name('rbac_admin')
        		->where(['username'=>$username])
        		->value('password');
        		
       if (!$pwd) {
            $this->error('用户名错误', url('./admin/index/index'), []);
        }
        if ($password!=$pwd) {
            $this->error('密码错误', url('./admin/index/index'), []);
        }
        
        $res=DB::query(" select DISTINCT controller_name,action_name from rbac_admin as a,rbac_admin_role as b,rbac_role_privilege as c,rbac_privilege as d where a.username='".$username."' and a.id=b.admin_id and b.role_id=c.role_id and c.pri_id=d.id");
      	
      	if(!$res){
        	$pri=array();
       }else{ 
        	$pri=array();
        	foreach($res as $k=>$v){
        		$pri[]="\\".$v['controller_name']."/".$v['action_name'];
        	}
        }
        
       if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),$pri)){
        	 $this->error('你没有权限登录', url('/admin'), []);
        }
       
        
       	session('admin_name', $username, 'admin');
        session('admin_pri', $pri, 'admin');
        
        $this->success('登陆成功', url('/main'), []);
        $this->redirect('/main');
    }

    /**
     * 后台登出处理
     * @return \think\response\View
     */
    public function logout()
    {
    	 Session::clear('admin');
        $this->redirect('/admin');
    }

}
